package com.lg.cms.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lg.cms.dto.PermissionDTO;
import com.lg.cms.entity.Permission;
import com.lg.cms.entity.RolePermission;
import com.lg.cms.mapper.PermissionMapper;
import com.lg.cms.mapper.RolePermissionMapper;
import com.lg.cms.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.cms.vo.admin.PermissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author lg
 * @since 2020-09-21
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    /**
     * 权限数据初始化
     */
//    @PostConstruct
    @Transactional
    @Override
    public void init() {
        //null为父级, 子级以父0, 1依次
        List<List<Permission>> permListS = new ArrayList<>();

        permListS.add(Arrays.asList(
                //大功能拓展小功能
                new Permission("测试权限", "测试权限", "/manger", "manger", null)
        ));

        permListS.add(Arrays.asList(
                //大功能拓展小功能
                new Permission("上传管理", "上传管理", null, null, null),
                new Permission("页面数据读取", "页面数据读取", "/admin/cloud/list", "upload:list", 0),
                new Permission("选择配置", "选择配置", "/admin/cloud/use", "upload:use", 1),
                new Permission("修改七牛云配置", "七牛云配置", "/admin/cloud/update-qiniuyun", "upload:updateQiniuyun", 1),
                new Permission("修改阿里云配置", "阿里云配置", "/admin/cloud/update-aliyun", "upload:updateAliyun", 1),
                new Permission("测试七牛云配置连通性", "测试七牛云配置连通性", "/admin/cloud/test-qiniuyun", "upload:testQiniuyun", 1),
                new Permission("测试阿里云配置连通性", "测试阿里云配置连通性", "/admin/cloud/test-aliyun", "upload:testAliyun", 1)
        ));
        int index = 1;
        for (List<Permission> permissions : permListS) {
            Permission parent0 = null;
            Permission parent1 = null;
            for (Permission permission : permissions) {
                permission.setId(index);
                Permission temp = permissionMapper.selectById(index);
                if (permission.getParentId() == null) {
                    if(temp == null) {
                        permissionMapper.insert(permission);
                        System.out.println("----------------------------" + permission);
                    }
                    parent0 = permission;
                } else if (permission.getParentId() == 0) {
                    if(temp == null) {
                        permission.setParentId(parent0.getId());
                        permissionMapper.insert(permission);
                    }
                    parent1 = permission;
                } else {
                    if(temp == null){
                        permission.setParentId(parent1.getId());
                        permissionMapper.insert(permission);
                    }

                }
                if (temp != null) {
                    temp.setName(permission.getName());
                    temp.setDescription(permission.getDescription());
                    temp.setUrl(permission.getUrl());
                    temp.setPerm(permission.getPerm());
                    if (permission.getParentId() == null) {
                        temp.setParentId(null);
                    } else if (permission.getParentId() == 0) {
                        temp.setParentId(parent0.getId());
                    } else {
                        temp.setParentId(parent1.getId());
                    }
                    permissionMapper.update(null, Wrappers.<Permission>lambdaUpdate()
                            .set(Permission::getName, temp.getName())
                            .set(Permission::getDescription, temp.getDescription())
                            .set(Permission::getUrl, temp.getUrl())
                            .set(Permission::getPerm, temp.getPerm())
                            .set(Permission::getParentId, temp.getParentId())
                            .eq(Permission::getId, temp.getId())
                    );
                }
                index += 1;
            }

        }
        permissionMapper.delete(new QueryWrapper<Permission>().gt("id", index - 1));
    }

    /**
     * 根据角色id获取权限列表
     * @param roleId
     * @return List<RolePermissionDTO>
     */
    @Override
    public List<PermissionVO> listByRoleId(Integer roleId) {
        //创建权限查询集合
        HashMap<Integer, Permission> permissionMap = new HashMap<>();
        //构造返回值对象
        List<PermissionVO> permissionVOList = new ArrayList<>();
        //构造角色对应权限的id列表
        List<Integer> permissionIdList = new ArrayList<>();
        permissionMapper.selectList(null).forEach(v -> permissionMap.put(v.getId(), v));
        //查询角色对应的权限id列表
        rolePermissionMapper.selectList
                (new QueryWrapper<RolePermission>().eq("role_id", roleId))
                .forEach(v -> permissionIdList.add(v.getPermId()));
        //循环角色对应的权限id列表
        for(Integer permissionId: permissionIdList){
            PermissionVO vo = new PermissionVO();
            Permission temp = permissionMap.get(permissionId);
//            dto.setRoleId(roleId);
//            dto.setPermissionId(permissionId);
            vo.setPermissionName(temp.getName());
            vo.setPermissionPerm(temp.getPerm());
            vo.setPermissionUrl(temp.getUrl());
//            vo.setTitle(vo.getPermissionName());
            permissionVOList.add(vo);
        }
        return permissionVOList;
    }
}
